package com.cxb.demotest.util;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Xbao
 * @version 1.0
 * @description
 * @date 2021/3/25 10:31
 */
public class PageUtil {

  /**
   * 分页
   * @param page
   * @param perPage
   * @param list
   * @return
   */
  public static List<?> getPagedList(int page, int perPage, List<?> list){

    if (page < 0 || (page-1) * perPage >= list.size() || perPage <= 0) {
      return new ArrayList<>();
    }
    int lastIndex = (page-1) * perPage + perPage;
    if (lastIndex > list.size()) {
      lastIndex = list.size();
    }

    //获得分页后的list
    list = list.subList((page-1) * perPage, lastIndex);

    return list;
  }

}